//
//  AllKindViewController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/30.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "AllKindViewController.h"
#import "AllTableViewCell.h"
#import "AllDataHandle.h"
#import "AllKindNextViewController.h"
#import "UserManager.h"

@interface AllKindViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *allPictureArray;

// 定义一个数字记录点击的cell
@property (nonatomic, assign) NSInteger clickRow;

@end

@implementation AllKindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 添加视图
    [self addView];
    [self searchNet];
    // 添加bar
    [self addBar];
    
    self.tabBarController.tabBar.hidden = YES;
    
    // 所有图片
    self.allPictureArray = [NSArray arrayWithObjects:@"caishicaipin.jpg", @"caixi.jpg", @"shilingshicai.jpg", @"gongxiao.jpg", @"changjing.jpg", @"gongyikouwei.jpg", @"caiyao.jpg", @"zhushi.jpg", @"xican.jpg", @"tanggengyinpin.jpg", @"qitacaipin1.jpg", @"renqun.jpg", @"jibing.jpg", @"churoulei.jpg", @"qindanlei.jpg", @"haixian.jpg", @"shucailei.jpg", @"shuiguolei.jpg", @"mimian.jpg", @"richang.jpg", @"qieri.jpg", @"qieqi1.jpg", @"jibengongyi.jpg", @"qitagongyi.jpg", @"jibenkouwei.jpg", @"duoyuankouwei.jpg", @"shuiguowei.jpg", @"tiaoweiliao.jpg", nil];
  //  NSLog(@"self.count = %ld", self.allPictureArray.count);
    
    
}

#pragma mark - 添加网络检测
- (void)searchNet
{
    NSLog(@"wangluo = %@", [[UserManager shareUserManagerHandle] getNet]);
    
    if ([[[UserManager shareUserManagerHandle] getNet] isEqualToString:@"NO"]) {
        // 网络不通
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"该软件只有在网络状态下才能使用" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:alertAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    } else {
        [[AllDataHandle shareAllDataHandle] requestAllDataAndReturn:^(NSArray *array) {
            [self.tableView reloadData];
        }];
    }
}


#pragma mark - 添加视图
- (void)addView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
   
    [self.view addSubview:_tableView];
   
    
    
}

#pragma mark - 添加bar
- (void)addBar
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(actionToReturn:)];
    leftItem.tintColor = [UIColor colorWithHue:0.8 saturation:1 brightness:0.7 alpha:1];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)actionToReturn:(UIBarButtonItem *)barButtonItem
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 必须写的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[AllDataHandle shareAllDataHandle] returnNumberOfCell];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *identifier = @"AllTableViewCell";
//    AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//  
//    
//    
//    if (cell == nil) {
//        cell = [[AllTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
//    }
    AllTableViewCell *cell = [[AllTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        cell.fetchArray = [[AllDataHandle shareAllDataHandle] returnArrayByIndexPath:indexPath];
    cell.titleString = [[AllDataHandle shareAllDataHandle] returnTitleByIndexPath:indexPath];
    // 给imageV添加手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToTapGes:)];
    [cell.pictureImageV addGestureRecognizer:tapGes];
    cell.pictureImageV.image = [UIImage imageNamed:self.allPictureArray[indexPath.row]];
    
    
    [cell.button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button1.tag = 1000;
    [cell.button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button2.tag = 1001;
    [cell.button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button3.tag = 1002;
    [cell.button4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button4.tag = 1003;
    [cell.button5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button5.tag = 1004;
    [cell.button6 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button6.tag = 1005;

    
    
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

#pragma mark - 图片的手势
- (void)actionToTapGes:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"执行手势");
}

#pragma mark - 按钮的点击方法
- (void)buttonAction:(UIButton *)button
{
    // 根据不同的button得到不同的indexpath.row
    UIView *v = [button superview];
    AllTableViewCell *cell = (AllTableViewCell *)[v superview];
    NSIndexPath *indexPathAll = [self.tableView indexPathForCell:cell];
 
 //  NSLog(@"得到的id是：%ld", [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:0 AndButtonsTag:1]);

    switch (button.tag) {
        case 1000:
        {
            NSLog(@"第一个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
            allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
            break;
        }
            case 1001:
        {
           NSLog(@"第二个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
            allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
            break;
        }
            case 1002:
        {
            NSLog(@"第三个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
            allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
            
            break;
        }
        case 1003:
        {
          NSLog(@"第四个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
             allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
            break;
        }
            case 1004:
        {
        NSLog(@"第五个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
              allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
          
            break;
        }
            case 1005:
        {
             NSLog(@"第六个button，在第%ld个cell上", (long)indexPathAll.row);
            NSLog(@"得到的id是：%ld", (long)[[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000]);
            AllKindNextViewController *allKindNextVC = [[AllKindNextViewController alloc] init];
            allKindNextVC.myID = [[AllDataHandle shareAllDataHandle] returnIntegerByIndexPath:indexPathAll.row AndButtonsTag:button.tag - 1000];
            [self.navigationController pushViewController:allKindNextVC animated:YES];
            break;
        }
        default:
            break;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130 * kWidthFitParameter;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
